// pages/mine/mine.ts
import { ajax } from "../../utils/request";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    listData: [
      { id: "001", title: "我的车牌", uri: "/pages/car/car", icon_path: "/assets/icon/feng.png", image_path: "/assets/icon/shuangzi.png" },
      { id: "002", title: "我的订单", uri: "/pages/order/order", icon_path: "/assets/icon/feng.png", image_path: "/assets/icon/fenghua.png" },
      { id: "003", title: "会员中心", uri: "/pages/vip/vip", icon_path: "/assets/icon/feng.png", image_path: "/assets/icon/keli.png" },
      { id: "009", title: "我的预约", uri: "/pages/book/book", icon_path: "/assets/icon/feng.png", image_path: "/assets/icon/duduke.png" },
      { id: "004", title: "我的卡券", uri: "/pages/coupon/coupon", icon_path: "/assets/icon/feng.png", image_path: "/assets/icon/hutao.png" },
      { id: "005", title: "月卡缴费", uri: "/pages/month/month", icon_path: "/assets/icon/feng.png", image_path: "/assets/icon/wendi.png" },
      { id: "006", title: "开具发票", uri: "/pages/invoice/invoice", icon_path: "/assets/icon/feng.png", image_path: "/assets/icon/zhongli.png" },
      { id: "007", title: "联系客服", uri: "/pages/consult/consult", icon_path: "/assets/icon/feng.png", image_path: "/assets/icon/baer.png" },
      { id: "008", title: "留言反馈", uri: "/pages/message/message", icon_path: "/assets/icon/feng.png", image_path: "/assets/icon/ningguang.png" }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (wx.getStorageSync("userInfo")) {
      this.setData({ userInfo: wx.getStorageSync("userInfo") })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  handleLogin() {

    // 获取code
    wx.login({
      success: async function (res) {
        console.log("login === ", res);
        // 发送code，获取sessionID
        let sessionRes = await ajax("/user/getSession", { code: res.code }, "GET")

        // 存储token值在本地缓存中
        wx.setStorageSync("sessionId", sessionRes.data.data);       
      }
    })

    if (this.data.userInfo === null || undefined) {
      wx.getUserProfile({
        desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res: any) => {

          console.log("getUser === ", res);
          // wx.setStorageSync("user", res);
          

          let param: any = {}
          // param.encryptedData = res.encryptedData;
          // param.iv = res.iv;
          param.nickname = res.userInfo.nickName;
          param.photo = res.userInfo.avatarUrl;
          param.sessionId = wx.getStorageSync("sessionId");

          ajax("/user/authLogin", param, "POST").then(res => {
            // 登录成功
            console.log("res =====", res);

            if (res.data.code === 0) {
              // 存储token值在本地缓存中
              wx.setStorageSync("token", res.header.token);
            }
            console.log('token======', wx.getStorageSync("token"));

          })

          // 模拟存在手机号
          // res.userInfo.phone = 13658105309
          // 模拟存在车牌号
          res.userInfo.carData = [
            { id: "001", num: "川A12345" },
            { id: "002", num: "新A91214" },
            { id: "003", num: "闽B66666" },
            { id: "004", num: "闽B66666" },
            { id: "005", num: "闽B66666" },
          ]
          this.setData({ userInfo: res.userInfo })

          // 用户信息缓存在本地
          let userInfo: any = res.userInfo
          wx.setStorageSync("userInfo", userInfo)
        }
      });
    } else {
      // console.log("有用户信息，则跳转至修改个人信息界面");
      // wx.navigateTo({
      //   url: "/pages/user/user"
      // })
      return
    }
    return;
  },
  // 前往个人信息修改页面
  edit(){
    wx.navigateTo({
      url: "/pages/user/user"
    })
  },

  // 调用获取手机号接口
  getPhoneNumber(e:WechatMiniprogram.CustomEvent) {
    console.log("eeeeeeeeeeeee",e);
    let param = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      sessionId: wx.getStorageSync("sessionId"),
      // code:e.detail.code
    }
    console.log("sessionId-p",wx.getStorageSync("sessionId"));
    
    console.log("getPhoneCode-form",param);
    ajax("/user/numberDecryption",param,"POST").then(res => {
      console.log("phoneRes",res);
      // this.data.userInfo.phone = res.data.data.telephone
      // console.log("userInfo-x",this.data.userInfo);
      let userInfo:any = {
        creditPoint:res.data.data.creditPoint,
        nickName:res.data.data.nickname,
        openid:res.data.data.openid,
        avatarUrl:res.data.data.photo,
        phone:res.data.data.telephone,
      }
      this.setData({userInfo})
      wx.setStorageSync("userInfo",userInfo)
    })
    
  },

  // 点击注销按钮，退出登录状态，清除本地缓存
  handleLogout() {
    // 弹框让用户确认
    wx.showModal({
      title: '旅行者要离开可莉嘛',
      content: '点击确认退出登录',
      success: res => {
        if (res.confirm) {
          wx.clearStorageSync()
          this.setData({ userInfo: null })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 子组件传来的值
  getInfo(v: any) {
    
    this.setData({ userInfo: v.detail })

  }

})